﻿using System;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using HYElectricCommon;
using Microsoft.Office.Interop.Word;

namespace HYRElectric.Calculate
{
	internal class WriteWord
	{
		public void Run()
		{
			Microsoft.Office.Interop.Word.Application application = (Microsoft.Office.Interop.Word.Application)Activator.CreateInstance(Marshal.GetTypeFromCLSID(new Guid("000209FF-0000-0000-C000-000000000046")));
			object value = Missing.Value;
			object obj = AssistFunc.GetAppRootPath() + "\\Sample\\照度计算书.dot";
			Microsoft.Office.Interop.Word._Document document = application.Documents.Add(ref obj, ref value, ref value, ref value);
			application.Visible = true;
			document.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;
			object obj2 = "标题 1";
			application.Selection.TypeText("照度计算计算书");
			application.Selection.ParagraphFormat.set_Style(ref obj2);
			object obj3 = "标题 2";
			application.Selection.TypeParagraph();
			application.Selection.TypeText("计算依据");
			application.Selection.ParagraphFormat.set_Style(ref obj3);
			application.Selection.TypeParagraph();
			application.Selection.TypeText("   灯具数量,照度值计算根据《照明设计手册》第二版(中国电力出版社) P221页公式(5-39)计算。\n   利用系数根据《照明设计手册》第二版(中国电力出版社)表4-1至4-62查询获得。");
			application.Selection.TypeParagraph();
			application.Selection.TypeText("计算结果");
			application.Selection.ParagraphFormat.set_Style(ref obj3);
			application.Selection.TypeParagraph();
			for (int i = 0; i < this.m_illCalData.arDGVData.Count; i++)
			{
				DGVRowData data = this.m_illCalData.arDGVData.ElementAt(i);
				this.WriteRoomInfo(application, data, i + 1);
				this.WriteLampInfo(application, data, i + 1);
				this.WriteLightSrcInfo(application, data, i + 1);
				this.WriteIllRequest(application, data, i + 1);
				this.WriteCalProcess(application, data, i + 1);
				this.WriteCheckResult(application, data, i + 1);
			}
		}

		private bool WriteRoomInfo(Microsoft.Office.Interop.Word.Application wordApp, DGVRowData data, int index)
		{
			object obj = "标题 3";
			wordApp.Selection.TypeText("空间编号:" + data.nFJBH.ToString() + " 空间名称:" + data.sFJMC);
			wordApp.Selection.ParagraphFormat.set_Style(ref obj);
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText(index.ToString() + ".1 空间条件");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    空间类型:" + data.sFJLX);
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    空间面积:" + data.dFJMJ.ToString("0.00") + "平方米");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    空间高度:" + data.dFJGD.ToString("0.00") + "米");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    工作面高度:" + data.dGZMGD.ToString("0.00") + "米");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    顶棚反射系数:" + data.dDPFSXS.dValue.ToString("0") + "%");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    墙面上部反射系数:" + data.dSBFSXS.dValue.ToString("0") + "%");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    墙面下部反射系数:" + data.dXBFSXS.dValue.ToString("0") + "%");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    地面反射系数:" + data.dDMFSXS.dValue.ToString("0") + "%");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    窗户面积:" + data.dCMJ.ToString("0.00"));
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    窗反射系数:" + data.dCFSXS.ToString("0") + "%");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeParagraph();
			return true;
		}

		private bool WriteLampInfo(Microsoft.Office.Interop.Word.Application wordApp, DGVRowData data, int index)
		{
			wordApp.Selection.TypeText(index.ToString() + ".2 灯具信息:");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    灯具维护系数:" + data.djcsData.dDJWHXS.ToString("0.00"));
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    灯具名称:" + data.djcsData.sDJMC);
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    灯具型号:" + data.djcsData.sDJXH);
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    灯具效率:" + data.djcsData.dDJXL.ToString("0.00"));
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    利用系数:" + data.djcsData.dLYXS.ToString("0.00"));
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    最大距高比A-A:" + data.djcsData.dAA.ToString("0.00"));
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    最大距高比B-B:" + data.djcsData.dBB.ToString("0.00"));
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText(string.Concat(new string[]
			{
				"    灯具镇流器使用:",
				data.djcsData.nZLQNum.ToString(),
				" × ",
				data.djcsData.dZLQGL.ToString("0.0"),
				"(W)"
			}));
			wordApp.Selection.TypeParagraph();
			return true;
		}

		private bool WriteLightSrcInfo(Microsoft.Office.Interop.Word.Application wordApp, DGVRowData data, int index)
		{
			wordApp.Selection.TypeText("    灯具使用光源:");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    光源生产厂家:" + data.gycsData.sSCCJ);
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    光源类型:" + data.gycsData.sGYLX);
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    光源型号:" + data.gycsData.sGYXH);
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    光源名称:" + data.gycsData.sGYMC);
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    光源数量:" + data.gycsData.nGYSL.ToString());
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    单个光源功率:" + data.gycsData.dGL.ToString("0"));
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    单个光源光通量:" + data.gycsData.dTGL.ToString("0"));
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeParagraph();
			return true;
		}

		private bool WriteIllRequest(Microsoft.Office.Interop.Word.Application wordApp, DGVRowData data, int index)
		{
			wordApp.Selection.TypeText(index.ToString() + ".3 照度要求:");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    照度要求:" + data.dPJZD.ToString("0"));
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    功率密度要求:" + data.dYQGLMD.ToString("0"));
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeParagraph();
			return true;
		}

		private bool WriteCalProcess(Microsoft.Office.Interop.Word.Application wordApp, DGVRowData data, int index)
		{
			wordApp.Selection.TypeText(index.ToString() + ".4 计算过程:");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    由 Eav = N×φ×U×K / A");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    得 N = (Eav×A) / (φ×U×K)");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    计算Eav");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    N = (Eav×A) / (φ×U×K)");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText(string.Concat(new string[]
			{
				"       = (",
				data.dPJZD.ToString("0"),
				"×",
				data.dFJMJ.ToString("0.00"),
				") / (",
				data.gycsData.dTGL.ToString("0"),
				"×",
				data.gycsData.nGYSL.ToString(),
				"×",
				data.djcsData.dLYXS.ToString("0.00"),
				"×",
				data.djcsData.dDJWHXS.ToString("0.00"),
				") = ",
				data.layoutData.nDJNum.ToString(),
				"套"
			}));
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeParagraph();
			return true;
		}

		private bool WriteCheckResult(Microsoft.Office.Interop.Word.Application wordApp, DGVRowData data, int index)
		{
			wordApp.Selection.TypeText(index.ToString() + ".5 校核结果:");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText(string.Concat(new string[]
			{
				"    采用本灯具:",
				data.layoutData.nDJNum.ToString(),
				"套    行数: ",
				data.layoutData.nRowNum.ToString(),
				"   列数: ",
				data.layoutData.nColNum.ToString()
			}));
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    边距比:" + data.layoutData.dBJB.ToString("0.00"));
			wordApp.Selection.TypeParagraph();
			string str = (data.layoutData.nVector == 0) ? "水平" : "垂直";
			wordApp.Selection.TypeText("    灯具布置方向:" + str);
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    实际照度 E = (N×φ×U×K) / A:");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText(string.Concat(new string[]
			{
				"               = (",
				data.layoutData.nDJNum.ToString(),
				"×",
				data.gycsData.dTGL.ToString("0"),
				"×",
				data.gycsData.nGYSL.ToString(),
				"×",
				data.djcsData.dLYXS.ToString("0.00"),
				"×",
				data.djcsData.dDJWHXS.ToString("0.00"),
				" / ",
				data.dFJMJ.ToString("0.00"),
				" = ",
				data.calResultData.dZDZ.ToString("0.00"),
				"(LX)"
			}));
			wordApp.Selection.TypeParagraph();
			if (data.calResultData.dZDZ < data.dPJZD * 0.9)
			{
				wordApp.Selection.TypeText("    ∵E < 0.9Eav, ∴计算照度未达到平均照度要求");
				wordApp.Selection.TypeParagraph();
			}
			else if (data.calResultData.dZDZ > data.dPJZD * 1.1)
			{
				wordApp.Selection.TypeText("    ∵E > 1.1Eav, ∴计算照度未达到平均照度要求");
				wordApp.Selection.TypeParagraph();
			}
			else
			{
				wordApp.Selection.TypeText("    ∵0.9Eav< E < 1.1Eav, ∴计算照度达到平均照度要求");
				wordApp.Selection.TypeParagraph();
			}
			if (data.calResultData.dAA > data.djcsData.dAA || data.calResultData.dAA < 0.01)
			{
				wordApp.Selection.TypeText("    ∵La-a > La-aMax\n    ∴ 灯具布置不合理");
				wordApp.Selection.TypeParagraph();
			}
			else if (data.calResultData.dBB > data.djcsData.dBB || data.calResultData.dBB < 0.01)
			{
				wordApp.Selection.TypeText("    ∵Lb-b > Lb-bMax\n    ∴ 灯具布置不合理");
				wordApp.Selection.TypeParagraph();
			}
			else
			{
				wordApp.Selection.TypeText("    ∵La-a < La-aMax     Lb-b < Lb-bMax\n    ∴ 灯具布置合理");
				wordApp.Selection.TypeParagraph();
			}
			wordApp.Selection.TypeText("    计算功率密度：W = 灯具数×(单个灯的镇流器功率＋单个灯的光源容量) / 面积");
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText(string.Concat(new string[]
			{
				"                    = ",
				data.layoutData.nDJNum.ToString(),
				"×(",
				data.djcsData.nZLQNum.ToString(),
				"×",
				data.djcsData.dZLQGL.ToString("0.0"),
				" + ",
				data.gycsData.dGL.ToString("0"),
				") / ",
				data.dFJMJ.ToString("0.00"),
				" = ",
				data.calResultData.dGLMD.ToString("0.00")
			}));
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    该建筑物：" + data.sJZWLX + "     " + data.sCSLX);
			wordApp.Selection.TypeParagraph();
			wordApp.Selection.TypeText("    要求的功率密度 X = " + data.dYQGLMD.ToString("0"));
			wordApp.Selection.TypeParagraph();
			if (data.calResultData.dZDZ <= 0.0001)
			{
				wordApp.Selection.TypeText("，《建筑照明设计标准》（GB 50034-2004）中无此建筑物的功率密度标准值");
				wordApp.Selection.TypeParagraph();
			}
			wordApp.Selection.TypeText("    换算到照度标准值的功率密度W(换算) = " + data.calResultData.dHSGLMD.ToString("0.00"));
			wordApp.Selection.TypeParagraph();
			if (data.calResultData.dZDZ >= 0.0001)
			{
				if (data.calResultData.dHSGLMD <= data.dYQGLMD)
				{
					wordApp.Selection.TypeText("    W(换算)  <= X 满足功率密度要求。");
					wordApp.Selection.TypeParagraph();
				}
				else
				{
					wordApp.Selection.TypeText("    W(换算)  > X 不满足功率密度要求。");
					wordApp.Selection.TypeParagraph();
				}
			}
			return true;
		}

		public IllCalData m_illCalData;
	}
}
